package;




#if (macro || display)
import haxe.macro.Expr;
import neko.FileSystem;
import neko.io.File;

import haxe.macro.Context;
import haxe.macro.Type;

#end
/**
 * ...
 * @author 
 */

class Implicit 
{
  
  @:macro public static function build():Array<Field>
  {
    var fe = Context.getBuildFields();

    //trace(Context.getLocalClass().get().meta.get());
    //Context.getLocalClass().get().meta.remove(":build");
    //trace(Context.getLocalClass().get().meta.get());
    var newF = [];
    
    for (f in fe) {
      if (f.name == "show") {
        f.meta = [( { pos:Context.currentPos(), params:[], name: ":macro" } )];
        //trace(f);
        switch (f.kind) {
          case FFun( _ ):
            var e = Context.parse("function ():haxe.macro.Expr.Expr { trace('hihihi');return {expr:haxe.macro.Expr.ExprDef.EBlock([]), pos:haxe.macro.Context.currentPos()};}", Context.currentPos());
            
            
            switch (e.expr) {
              case EFunction(_, f1): 
                trace("yes");
                f.kind = FFun(f1);
              default:
            }
          default: 
        } 
        trace(f);
        newF.push(f);
      }else if (f.name == "check") {
          newF.push(f);
        }
    }
    FileSystem.rename("Uhu.hx", "Uhu.hx_");
    // get Uhu in macro mode
    var out = File.write("MyTest.hx", false);
    out.writeString("private class Blub { @:macro public static function getType():haxe.macro.Type { "
    + " Uhu; return haxe.macro.Context.getType('Float');}} private typedef Blub2 = haxe.macro.MacroType<(_MyTest.Blub.getType())>;");
    out.close();
    FileSystem.rename("UhuM.hx", "Uhu.hx");
    Context.getModule("MyTest");
    FileSystem.rename("Uhu.hx", "UhuM.hx");
    FileSystem.deleteFile("MyTest.hx");
    FileSystem.rename("Uhu.hx_", "Uhu.hx");
    return newF;
  } 
}